package com.kurtphpr.sistema.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.kurtphpr.sistema.cliente.Cliente;
import com.kurtphpr.sistema.hibernate.hibernateUtil;

public class ClienteDAO {

	private Session sessao;
	private Transaction tr;
	private List<Cliente> list;

	
	
	
	@SuppressWarnings("unchecked")
	public List<Cliente> getList() {
		
		sessao = hibernateUtil.getSessionFactory().openSession();
		tr = sessao.beginTransaction();
		
		Criteria cri = sessao.createCriteria(Cliente.class);
		this.list = cri.list();
		return list;
	}
	
	

	public void salvar(Cliente c) {
		try {

			sessao = hibernateUtil.getSessionFactory().openSession();
			tr = sessao.beginTransaction();
			Cliente cliente = new Cliente();
			cliente.setCpf(c.getCpf());
			cliente.setEmail(c.getEmail());

			sessao.save(cliente);
			tr.commit();

		} catch (HibernateException e) {
			e.printStackTrace();
			System.out.println("Deu ruim no salvar!!!" + e.getMessage());
			// TODO: handle exception
		} finally {
			sessao.close();
		}
	}
	
	public void remover(Cliente c) {
		try {

			sessao = hibernateUtil.getSessionFactory().openSession();
			tr = sessao.beginTransaction();

			sessao.delete(c);
			tr.commit();

		} catch (HibernateException e) {
			e.printStackTrace();
			System.out.println("Deu ruim no remover!!!" + e.getMessage());
			// TODO: handle exception
		} finally {
			sessao.close();
		}
	}
	
	public void atualizar(Cliente c) {
		try {

			sessao = hibernateUtil.getSessionFactory().openSession();
			tr = sessao.beginTransaction();

			sessao.update(c);
			tr.commit();

		} catch (HibernateException e) {
			e.printStackTrace();
			System.out.println("Deu ruim no remover!!!" + e.getMessage());
			// TODO: handle exception
		} finally {
			sessao.close();
		}
	}

}
